from app.models import db
import enum
from datetime import datetime
class User(db.Model):
    __tablename__ = 'user'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(255), unique=True, nullable=False)
    password = db.Column(db.String(60), nullable=False)
    nickname = db.Column(db.String(255))
    email = db.Column(db.String(255))
    phone = db.Column(db.String(11))

    # 用于debug
    def __repr__(self):
        return '<User %r>' % self.username


# 先定义 Python 枚举类以支持 riskpreference 字段
class RiskPreferenceEnum(enum.Enum):
    低风险 = '低风险'
    中风险 = '中风险'
    高风险 = '高风险'
class Preference(db.Model):
    __tablename__ = 'investment_preferences'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment="序号")

    userid = db.Column(db.Integer, unique=True, nullable=False, comment="对应用户")

    riskpreference = db.Column(
        db.Enum(RiskPreferenceEnum), nullable=False, comment="低/中/高风险"
    )

    exp = db.Column(
        db.Integer,
        nullable=False,
        comment="投资经验 1234代表四个选项",
    )

    maxloss = db.Column(
        db.Integer,
        nullable=False,
        comment="接受的最大单次损失 1234",
    )

    revenue = db.Column(
        db.Integer,
        nullable=False,
        comment="年收入 1234",
    )

    goal = db.Column(
        db.Integer,
        nullable=False,
        comment="投资目标 1234",
    )

    # check 约束
    __table_args__ = (
        db.CheckConstraint('exp > 0 AND exp <= 4', name='check_exp_range'),
        db.CheckConstraint('maxloss > 0 AND maxloss <= 4', name='check_maxloss_range'),
        db.CheckConstraint('revenue > 0 AND revenue <= 4', name='check_revenue_range'),
        db.CheckConstraint('goal > 0 AND goal <= 4', name='check_goal_range'),
        {'comment': '风险偏好表'},
    )

    # 用于debug
    def __repr__(self):
        return '<Preference %r>' % self.username

# 定义资产种类的枚举
class AssetTypeEnum(enum.Enum):
    股票 = '股票'
    债券 = '债券'
    基金 = '基金'
    外汇 = '外汇'

class UserAsset(db.Model):
    __tablename__ = 'assets'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment="序号")
    products_id = db.Column(db.String(20), nullable=False, comment="产品标识码")
    products_name = db.Column(db.String(20), nullable=False, comment="产品名字")
    userid = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False, comment="对应用户")
    type = db.Column(db.Enum(AssetTypeEnum), nullable=False, comment="资产种类 股票 债券 基金 外汇")
    quantity = db.Column(db.Integer,nullable=False,comment="持仓数量")
    buy_price = db.Column(db.Float(precision=2), nullable=False, comment="买入价格")


    def __repr__(self):
        return f'<UserAsset {self.products_name} ({self.type.value}) - User {self.userid}>'

# 交易类型
class TradeTypeEnum(enum.Enum):
    买入 = '买入'
    售出 = '售出'

class Transaction(db.Model):
    __tablename__ = 'transactions'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment="序号")
    products_id = db.Column(db.String(20), nullable=False, comment="产品标识码")
    products_name = db.Column(db.String(20), nullable=False, comment="产品名字")
    userid = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False, comment="对应用户")
    type = db.Column(db.Enum(AssetTypeEnum), nullable=False, comment="资产种类 股票 债券 基金 外汇")
    trade_type = db.Column(db.Enum(TradeTypeEnum), nullable=False, comment="买入还是售出")
    quantity = db.Column(db.Integer, nullable=False, comment="买卖数量")
    trade_time = db.Column(db.DateTime, nullable=False, default=datetime.utcnow, comment="交易时间，若不提供则为当前时间戳")
    trade_amount = db.Column(db.Float(precision=2), nullable=False, comment="交易金额（买入或卖出总金额）")
    fee = db.Column(db.Float(precision=2), default=0, comment="手续费")

    def __repr__(self):
        return f'<Transaction {self.trade_type.value} {self.products_name} ({self.quantity}) by User {self.userid}>'


# 定义 LTVLevel 枚举类
class LTVLevelEnum(enum.Enum):
    LOW = 'LOW'
    MEDIUM = 'MEDIUM'
    HIGH = 'HIGH'


class UserBehaviorStats(db.Model):
    __tablename__ = 'user_behavior_stats'

    id = db.Column(
        db.Integer,
        primary_key=True,
        autoincrement=True,
        comment="序号"
    )

    user_id = db.Column(
        db.Integer,
        nullable=False,
        comment="用户唯一标识"
    )

    registration_date = db.Column(
        db.Date,
        nullable=False,
        comment="注册日期"
    )

    total_login_count = db.Column(
        db.Integer,
        default=0,
        comment="累计登录次数"
    )

    total_transactions = db.Column(
        db.Integer,
        default=0,
        comment="累计交易次数"
    )

    total_commission = db.Column(
        db.Numeric(12, 2),
        default=0.00,
        comment="累计佣金费用"
    )

    ltv = db.Column(
        db.Enum(LTVLevelEnum),
        comment="等级"
    )

    # 用于debug
    def __repr__(self):
        return f'<UserBehaviorStats user_id={self.user_id} ltv={self.ltv}>'